package org.cocos2dx.javascript.admob;

import android.app.Activity;
import android.util.Log;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
import com.logicgame.carmerge.R;

import androidx.annotation.NonNull;

public class Reward {
    static Activity app;
    static int res = 0;
    private static RewardedAd rewardedAd;
    private static final String TAG = "Admob";
    public static void InitrewardedAD(final Activity _app){
        app = _app;
        final String REWARD_AD_UNIT_ID = app.getString(R.string.REWARD_AD_UNIT_ID);
        if (rewardedAd == null) {
            AdRequest adRequest = new AdRequest.Builder().build();
            RewardedAd.load(
                    app,
                    REWARD_AD_UNIT_ID,
                    adRequest,
                    new RewardedAdLoadCallback() {
                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                            // Handle the error.
                            Log.d(TAG, loadAdError.getMessage());
                            rewardedAd = null;
                            Ad_Fail_Timer.reload(3,app);
                            //MainActivity.this.isLoading = false;
                            //Toast.makeText(MainActivity.this, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onAdLoaded(@NonNull RewardedAd _rewardedAd) {
                            rewardedAd = _rewardedAd;
                            Log.d(TAG, "RewardAD onAdLoaded");
                            //MainActivity.this.isLoading = false;
                            //Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }
    public static int GetRewardAD(){
        if(rewardedAd == null){
            return 0;
        }else{
            return 1;
        }
    }


    /**
     * @return
     * 0:代表无广告显示
     * 1：代表广告正常显示完毕，需要发放奖励
     */
    public static int ShowRewardAD(){
        if (rewardedAd == null) {
            Log.d("TAG", "The rewarded ad wasn't ready yet.");
            return 0;
        }
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.v(TAG,"show rewardAD in admob.reward");
                rewardedAd.setFullScreenContentCallback(
                        new FullScreenContentCallback() {
                            @Override
                            public void onAdShowedFullScreenContent() {
                                // Called when ad is shown.
                                Log.d(TAG, "onAdShowedFullScreenContent");
                                //Toast.makeText(MainActivity.this, "onAdShowedFullScreenContent", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdFailedToShowFullScreenContent(AdError adError) {
                                // Called when ad fails to show.
                                Log.d(TAG, "onAdFailedToShowFullScreenContent");
                                // Don't forget to set the ad reference to null so you
                                // don't show the ad a second time.
                                rewardedAd = null;
                                Ad_Fail_Timer.reload(3,app);
                                //Toast.makeText(MainActivity.this, "onAdFailedToShowFullScreenContent", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Called when ad is dismissed.
                                // Don't forget to set the ad reference to null so you
                                // don't show the ad a second time.
                                rewardedAd = null;
                                Log.d(TAG, "onAdDismissedFullScreenContent");
                                //Toast.makeText(MainActivity.this, "onAdDismissedFullScreenContent", Toast.LENGTH_SHORT).show();
                                // Preload the next rewarded ad.
                                Ad_Fail_Timer.reload(3,app);
                            }
                        });
                Activity activityContext = app;
                rewardedAd.show(
                        activityContext,
                        new OnUserEarnedRewardListener() {
                            @Override
                            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                                // Handle the reward.
                                Log.d("TAG", "The user earned the reward.");
                                //int rewardAmount = rewardItem.getAmount();
                                //String rewardType = rewardItem.getType();
                                res = 1;
                            }
                        });
            }
        });
        return res;
    }
}
